package edu.isquen.mobile.action;

import edu.isquen.core.base.action.BaseAction;
import edu.isquen.core.user.model.User;
import edu.isquen.core.user.model.UserRoleSet;
import edu.isquen.core.user.service.UserService;
import edu.isquen.util.factory.ServiceFactory;

public class loginAction extends BaseAction<User> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1553740852130269460L;
	
	private String roleid;
	
	private String strResult;
	
	public loginAction() throws Exception {
		super(ServiceFactory.createProcess(UserService.class), new User());
	}
	
	public String doSave(){
		User user=(User)this.getValObj();
		try {
			if(this.getRoleid()!=null){
				UserRoleSet urs=new UserRoleSet(user.getId(), this.getRoleid());
				user.getUserRoleSet().add(urs);
			}
			((UserService) service).doSaveOrUpdate(user);
		} catch (Exception e) {
			this.addActionMessage("Exception: "+e.getMessage());
			return INPUT;
		}
		this.addActionMessage("Add User SUCCESS!");
		return SUCCESS;
	}
	
	public String doLogin(){
		User user=(User)this.getValObj();
		try {
			User loginUser=((UserService) service).doLogin(user.getLoginno(), user.getLoginpwd());
			if(loginUser==null){
				this.setStrResult("Exception: The name or passworld is error!!!");
				return INPUT;
			}
			this.putIntoSession(User.front_USER, loginUser);
			this.setStrResult("Login success!");
		} catch (Exception e) {
			this.setStrResult("Exception: "+e.getMessage());
			return INPUT;
		}
		return SUCCESS;
	}
	
	public String doLoginout(){
		this.removeFromSession(User.front_USER);
		return SUCCESS;
	}
	
	public String getRoleid() {
		return roleid;
	}

	public void setRoleid(String roleid) {
		this.roleid = roleid;
	}
	
	public String getStrResult() {
		return strResult;
	}

	public void setStrResult(String strResult) {
		this.strResult = strResult;
	}
}
